package com.xxz.course;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class TransportActivity extends AppCompatActivity {
    private  static final String TAG="Rate";
    EditText dollarText;
    EditText euroText;
    EditText wonText;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transport);

        Intent intent = getIntent();
        float dollar2 = intent.getFloatExtra("dollar_key",0.0f);
        float euro2 = intent.getFloatExtra("euro_key",0.0f);
        float won2 = intent.getFloatExtra("won_key",0.0f);

        Log.i(TAG,"onCreate: dollar2="+dollar2);
        Log.i(TAG,"onCreate: euro2="+euro2);
        Log.i(TAG,"onCreate: won2="+won2);

        dollarText = (EditText)findViewById(R.id.dollar_rate);
        euroText = (EditText)findViewById(R.id.euro_rate);
        wonText = (EditText)findViewById(R.id.won_rate);

        dollarText.setText(""+dollar2);
        euroText.setText(""+euro2);
        wonText.setText(""+won2);
    }
    public void save(View btn){
        Log.i(TAG,"save:");
        //获取新的值
        float newDollar = Float.parseFloat(dollarText.getText().toString());
        float newEuro = Float.parseFloat(euroText.getText().toString());
        float newWon = Float.parseFloat(wonText.getText().toString());

        Log.i(TAG,"save: 获取到新的值");
        Log.i(TAG,"save: newDollar= "+newDollar);
        Log.i(TAG,"save: newEuro= "+newEuro);
        Log.i(TAG,"save: newWon= "+newWon);


        //保存到Bundle或放入到Extra
        Intent intent = getIntent();
        Bundle bdl = new Bundle();
        bdl.putFloat("key_dollar",newDollar);
        bdl.putFloat("key_euro",newEuro);
        bdl.putFloat("key_won",newWon);
        intent.putExtras(bdl);
        setResult(2,intent);

        //返回到调用页面
        finish();

       /* SharedPreferences sp =getSharedPreferences("myrate",Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        EditText d = findViewById(R.id.dl);
        float d1 = Float.parseFloat(d.getText().toString());
        EditText e = findViewById(R.id.er);
        float e1 = Float.parseFloat(e.getText().toString());
        EditText w = findViewById(R.id.wn);
        float w1 = Float.parseFloat(w.getText().toString());
        editor.putFloat("dollar_rate",d1);
        editor.putFloat("euro_rate",e1);
        editor.putFloat("won_rate",w1);
        finish();
        float dy,ey,wy;
        Intent intent = getIntent();
        float dollar2 = intent.getFloatExtra("dollar_key",0.0f);
        float euro2 = intent.getFloatExtra("euro_key",0.0f);
        float won2 = intent.getFloatExtra("won_key",0.0f);
        EditText d = findViewById(R.id.dl);
        String d1 = d.getText().toString();
        if(d1.length()==0)
        {
             dy=dollar2;
        }
        else
        {
             dy = Float.parseFloat(d.getText().toString());
        }
        EditText e = findViewById(R.id.er);
        String e1 = e.getText().toString();
        if(e1.length()==0)
        {
             ey=euro2;
        }
        else
        {
             ey = Float.parseFloat(e.getText().toString());
        }
        EditText w = findViewById(R.id.wn);
        String w1 = w.getText().toString();
        if(w1.length()==0)
        {
             wy=won2;
        }
        else
        {
             wy = Float.parseFloat(w.getText().toString());
        }
        Intent intent1 = new Intent(this,ConvertActivity.class);
        intent1.putExtra("dollar_key2",dy);
        intent1.putExtra("euro_key2",ey);
        intent1.putExtra("won_key2",wy);
        setResult(6,intent1);
        finish();
         */
    }
}